#if !defined(_GNU_SOURCE)
        #define _GNU_SOURCE
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/msg.h>
#include <limits.h>
#include <fcntl.h>
#include <syslog.h>
#include <errno.h>
#include <pwd.h>
#include <signal.h>
#include <time.h>
#include <execinfo.h>
#include <wait.h>

//ini parser header
#include "ini.h"

/* Daemon name */
static const char DAEMON_NAME[] = "logd";

/* run user */
#define RUN_AS_USER "root"
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

/* lock file fd */
int f_lock=-1;
/* log file fd */
int f_log=-1;
/* MQ QID */
int msgQID;
/* current log file name */
char logfile[256];
/* pid-file name*/
char pidfile[256];

/* signal related variables*/
struct sigaction sa;
sigset_t 		 ss;

/* Config file*/
typedef struct
{
	const char* general_servername;
    const char* lockfile_path;
    int		mq_key;
    int		mq_msgsize; //in kb
    int		mq_syssize; //in kb
    const char* logfile_path;
    int		logfile_perm;
    const char* logfile_behavior;
    int		logfile_maxsize; //in Mb
    const char*	db_host;
    int		db_port;
    const char* db_name;
    const char* db_user;
    const char* db_pass;
} configuration;
configuration config;

/* MQ message structure */
typedef struct Message{
    long    type;
    char    text[8192];
} Message;

//static void lock(void);
static void daemonize(void);

//main function
void work_mq(void);

//mq prepare (creation, checking, sanity)
void prepare_mq(void);

//cleaner and closer
static void finalizer(void);

//set log filename as 'unix timestamp'.log
static void setLogname(void);

//create and lock pid file, write child pid to it
static void createPidFile(pid_t);

